﻿var token = localStorage.getItem("token");
$(document).ready(function () {
    ShowTime();
});
var datalists = [];
var Datetimenow = new Date();
var year = Datetimenow.getFullYear() + '-' + (Datetimenow.getMonth() + 1) + '-' + Datetimenow.getDate() + ' ' + Datetimenow.getHours() + ':' + Datetimenow.getMinutes() + ':' + Datetimenow.getSeconds();
function ShowTime() {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;
    var ids = [];
    var cheddata = [];
    // 表单提交事件
    layui.use('table', function () {
        var table = layui.table;
        var layer = layui.layer;

        //渲染表格的函数
        function renderTable(pageNum) {
            //获取查询条件
            var MovelibraryCode = $("#MovelibraryCode").val();
            var MovelibraryType = $("#MovelibraryType").val();
            var DocumentStatus = $("#DocumentStatus").val();
            let showmovelibrary = {
                MovelibraryCode: MovelibraryCode,
                MovelibraryType: MovelibraryType,
                DocumentStatus: DocumentStatus,
                Office_Id: "1859803506630856704",
                PageIndex: pageNum,
                PageSize: 100,
                Token: token
            };
            // 新库位
            let movelocationlist = {
                3: '默认报损库位',
                4: '报损库位',
                5: '报损库位',
                6: '报损库位',
                7: '报损仓库',
                8: '报损库位',
                9: '默认库位',
                11: '默认新增库位',
                24: '新品仓库',
                41: '废品库位'
            };
            // 移库类型
            let movelibraryTypelist = {
                1: "移库上架",
                2: "仓库移库",
                3: "报损移库",
            };
            // 审核状态
            let documentStatuslist = {
                1: "待审核",
                2: "审核通过",
                3: "审核驳回",
            };

            //发送AJAX请求
            $.ajax({
                url: '/Movelibrary/GetMovelibraryShow',
                type: 'get',
                data: showmovelibrary,
                dataType: 'json',
                success: function (res) {
                    var data = JSON.parse(res)
                    console.log(data)
                    //渲染表格
                    table.render({
                        elem: '#dataTable',
                        cols: [[
                            { type: 'checkbox', fixed: 'left' }, // 启用多选框
                            { field: 'movelibraryCode', title: '移库单号' },
                            { field: 'productsName', title: '产品名称' },
                            { field: 'productsCode', title: '产品编号' },
                            { field: 'specs', title: '规格' },
                            { field: 'moveProductNum', title: '数量' },
                            { field: 'warehouseName', title: '仓库' },
                            { field: 'storehouseName', title: '原库位' },
                            {
                                field: 'movelocation', title: '新库位', templet: function (d) {
                                    return movelocationlist[d.movelocation];
                                }
                            },
                            {
                                field: 'movelibraryType', title: '移库类型',
                                templet: function (d) {
                                    return movelibraryTypelist[d.movelibraryType];
                                }
                            },
                            {
                                field: 'documentStatus', title: '状态',
                                templet: function (d) {
                                    return documentStatuslist[d.documentStatus];
                                }
                            },
                            {
                                title: '操作',
                                templet: function (d) {
                                    if (d.documentStatus == 1) {
                                        //编辑
                                        return '<a lay-on="test-page" id="detail" data-movelibraryId="' + d.movelibraryId + '">查看</a>' +
                                            ' <a lay-on="test-fan" class="fan" href="/Movelibrary/UpdMovelibrary?MovelibraryId=' + d.movelibraryId + '">编辑 </a>' +
                                            ' <a onclick="deleteItem(' + d.movelibraryId + ')">删除</a>' +
                                            ' <a lay-on="test-ru" id="he" data-movelibraryId="' + d.movelibraryId + '" data-inProductId="' + d.productsID + '">审核 </a>'
                                    }
                                    else {
                                        //编辑
                                        return '<a lay-on="test-page" id="detail" data-movelibraryId="' + d.movelibraryId + '">查看</a>' +
                                            ' <a lay-on="test-fan" class="fan" href="/Movelibrary/UpdMovelibrary?MovelibraryId=' + d.movelibraryId + '">编辑 </a>' +
                                            ' <a onclick="deleteItem(' + d.movelibraryId + ')">删除</a>'
                                    }
                                }
                            },
                        ]],
                        data: data.data.list,//数据源
                        page: true,
                        limit: 10,
                        limits: [10, 20, 30],
                        curr: pageNum,
                        total: data.data.lcount,  // 总记录数
                        loading: true, // 启用加载状态
                        toolbar: "#toolbarDemo",
                        defaultToolbar: ["exports"]
                    });
                },
                Error: function (xhr, status, error) {
                    console.log("请求失败" + error);
                }
            });
        }
        // 初次加载表格数据
        renderTable(1);
        // 查询按钮点击事件
        $('#searchBtnMovelibrary').click(function () {
            renderTable(1);  // 重置为第一页，并重新加载数据
        });
        // 触发表格复选框选择
        table.on('checkbox(dataTableFilter)', function (obj) {
            cheddata = table.checkStatus('dataTable').data;
            ids = cheddata.map(function (item) {
                return item.moveProductId;
            })
            console.log(ids);
        });
        ///批量删除
        $('#BathMovelibraryDel').click(function () {
            if (ids.length == 0) {
                layer.msg('请先选择要删除的项', { icon: 2 });
                return;
            }
            // 询问是否删除
            layer.confirm('确定删除吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                let delmovelibrary = {};
                delmovelibrary.MovelibraryId = ids;
                delmovelibrary.DelName = "呓语";
                delmovelibrary.DelTime = year;
                delmovelibrary.Token = token;
                //执行删除操作
                $.ajax({
                    url: '/Movelibrary/DelAllMovelibrary',
                    type: 'post',
                    data: delmovelibrary,
                    success: function (res) {
                        var s = JSON.parse(res)
                        if (s.data > 0) {
                            layer.msg('删除成功', { icon: 1 }, function () {
                                location.reload()
                            });
                        }
                        else {
                            layer.msg('删除失败');
                            return
                        }
                    }
                })
            }, function () {
                layer.msg('已取消', {
                    time: 20000, //20s后自动关闭
                    btn: ['确定']
                });
            });
        });

        //刷新
        $("#Vendorrefresh").click(function () {
            location.reload()
        })

        //查看详情
        $(document).on('click', '#detail', function () {
            var movelibraryId = $(this).attr('data-movelibraryId');  // 获取按钮上设置的采购ID
            console.log(movelibraryId);
            //打开弹框
            var index = layer.open({
                type: 1,
                area: ['800px', '500px'], // 宽高
                content: '<table style="width:650px;height:300px; margin-left:45px">' +
                    '<tr>' +
                    '<td>移库单号：<span id="MovelibraryCode1"></span></td>' +
                    '<td>移库类型：<span id="MovelibraryType1"></span></td>' +
                    '<td>单据状态：<span id="DocumentStatus1"></span></td>' +
                    '</tr>' +
                    '<tr>' +
                    '<td>关联单号：<span id="MorderNumber"></span></td>' +
                    '<td>制单人：<span id="MakePerson"></span></td>' +
                    '<td>制单日期：<span id="MarkTime1"></span></td>' +
                    '</tr>' +
                    '<tr>' +
                    '<td>备注：<span id="ORremark"></span></td>' +
                    '<td>审核原因：<span id="ReasonForReview"></span></td>' +
                    '<td></td>' +
                    '</tr>' +
                    '</table>' +
                    '<table class= "layui-table" id="Movelibrary-Id" >' +
                    '<thead>' +
                    '<tr>' +
                    '<col width="150">' +
                    '<col>' +
                    '</tr>' +
                    '</thead>' +
                    '</table>',

                success: function (lay, index) {
                    form.render();
                }
            });
            var fmovelibrary = {
                Token: token,
                MovelibraryId: movelibraryId
            }
            $.ajax({
                url: '/Movelibrary/GetMovelibraryFt',
                type: "get",
                data: fmovelibrary,
                success: (ress) => {
                    var res = JSON.parse(ress);
                    console.log(res)
                    // 渲染表格
                    table.render({
                        elem: '#Movelibrary-Id',
                        cols: [[
                            { field: 'productsCode', title: '产品编号', width: 150 },
                            { field: 'productsName', title: '产品名称', width: 150 },
                            { field: 'specs', title: '规格', width: 150 },
                            { field: 'moveProductNum', title: '数量', width: 150 },
                            {
                                field: 'storehouseId',
                                title: '当前库位',
                                width: 120,
                                templet: function (d) {
                                    switch (d.storehouseId) {
                                        case 7:
                                            return '报损仓库';
                                        case 8:
                                            return '报损库位';
                                        case 9:
                                            return '默认库位';
                                        default:
                                            return '未知类型';
                                    }
                                }
                            },
                            {
                                field: 'movelocation', title: '移入库位', width: 150
                            },
                        ]],
                        data: res.data[0].listProduct  // 数据源
                    });
                    //反填移库信息
                    $(res.data).each(function () {
                        var date = new Date(this.markTime);

                        // 获取年、月、日
                        var year = date.getFullYear();
                        var month = (date.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，因此加1
                        var day = date.getDate().toString().padStart(2, '0');  // 补充两位数字
                        // 拼接成 "yyyy-MM-dd" 格式
                        var formattedDate = `${year}-${month}-${day}`;
                        $("#MovelibraryCode1").text(this.movelibraryCode);
                        $("#MovelibraryType1").text(this.movelibraryType);
                        $("#DocumentStatus1").text(this.documentStatus);
                        $("#MorderNumber").text(this.morderNumber);
                        $("#MarkTime1").text(formattedDate);
                        $("#MakePerson").text(this.makePerson);
                        $("#ORremark").text(this.oRremark);
                        $("#ReasonForReview").text(this.reasonForReview);
                    })
                }
            })
        })
        //删除
        window.deleteItem = function (movelibraryId) {
            var layer = layui.layer;
            layer.confirm('你确定要删除这条数据吗？', {
                title: '提示'
            }, function (index) {
                let delmovelibrary = {};
                delmovelibrary.MovelibraryId = movelibraryId;
                delmovelibrary.DelName = "呓语";
                delmovelibrary.DelTime = year;
                delmovelibrary.Token = token;
                //执行删除操作
                $.ajax({
                    url: '/Movelibrary/DelAllMovelibrary',
                    type: 'post',
                    data: delmovelibrary,
                    success: function (res) {
                        var s = JSON.parse(res)
                        if (s.data > 0) {
                            layer.msg('删除成功', { icon: 1 }, function () {
                                location.reload()
                            });
                        }
                        else {
                            layer.msg('删除失败');
                            return
                        }
                    }
                })
            });
        };
    });



    $(document).on('click', "#he", function () {
        var movelibraryId = $(this).attr('data-movelibraryId');  // 获取按钮上设置的采购ID
        var inProductId = $(this).attr('data-inProductId');  // 获取按钮上设置的采购ID
        //打开弹框
        var index = layer.open({
            type: 1,
            area: ['800px', '500px'], // 宽高
            content: '<table style="width:650px;height:300px; margin-left:45px">' +
                '<tr>' +
                '<td>移库单号：<span id="MovelibraryCodes"></span></td>' +
                '<td>移库类型：<span id="MovelibraryTypes"></span></td>' +
                '<td>单据状态：<span id="DocumentStatuss"></span></td>' +
                '</tr>' +
                '<tr>' +
                '<td>关联单号：<span id="MorderNumbers"></span></td>' +
                '<td>制单人：<span id="MakePersons"></span></td>' +
                '<td>制单日期：<span id="MarkTimes"></span></td>' +
                '</tr>' +
                '<tr>' +
                '<td>备注：<span id="ORremarks"></span></td>' +
                '<td>审核原因：</td>' +
                '<td><textarea placeholder="请输入" class="layui-textarea"  id="ReasonForReviews"></textarea></td>' +
                '<td></td>' +
                '</tr>' +
                '<table class= "layui-table" id="Movelibrary-sId" >' +
                '<thead>' +
                '<tr>' +
                '<col width="150">' +
                '<col>' +
                '</tr>' +
                '</thead>' +
                '</table>' +
                '<button type="button" class="layui-btn layui-btn-primary "  id="yes">审核通过</button>' +
                '<button type="button" class="layui-btn layui-btn-primary " id="no">审核不通过</button>',

            success: function (lay, index) {
                form.render();
            }
        });

        //反填
        var fmovelibrary = {
            Token: token,
            MovelibraryId: movelibraryId
        }
        $.ajax({
            url: '/Movelibrary/GetMovelibraryFt',
            type: "get",
            data: fmovelibrary,
            success: (ress) => {
                var res = JSON.parse(ress);
                console.log(res);

                // 渲染表格
                table.render({
                    elem: '#Movelibrary-sId',
                    cols: [[
                        { field: 'productsCode', title: '产品编号', width: 150 },
                        { field: 'productsName', title: '产品名称', width: 150 },
                        { field: 'specs', title: '规格', width: 150 },
                        { field: 'moveProductNum', title: '数量', width: 150 },
                        {
                            field: 'storehouseId',
                            title: '当前库位',
                            width: 120,
                            templet: function (d) {
                                switch (d.storehouseId) {
                                    case 7:
                                        return '报损仓库';
                                    case 8:
                                        return '报损库位';
                                    case 9:
                                        return '默认库位';
                                    default:
                                        return '未知类型';
                                }
                            }
                        },
                        {
                            field: 'movelocation', title: '移入库位', width: 150
                        },
                    ]],
                    data: res.data[0].listProduct  // 数据源
                });

                //反填移库信息
                $(res.data).each(function () {
                    $("#MovelibraryCodes").text(this.movelibraryCode);
                    $("#MovelibraryTypes").text(this.movelibraryType);
                    $("#DocumentStatuss").text(this.documentStatus);
                    $("#MorderNumbers").text(this.morderNumber);
                    $("#MarkTimes").text(this.markTime);
                    $("#MakePersons").text(this.makePerson);
                    $("#ORremarks").text(this.oRremark);
                })
            }
        })



        //审核通过
        $(document).on('click', "#yes", function () {
            var shenhe = {
                MovelibraryId: movelibraryId,
                DocumentStatus: 2,
                ReasonForReview: $("#ReasonForReviews").val() || '',
                office_id: "1859803506630856704",
                user_id: 3,
                Token: token
            }
            $.ajax({
                url: '/Movelibrary/AuditMovelibrary',
                type: 'post',
                data: shenhe,
                success: function (d) {
                    var res = JSON.parse(d);
                    console.log(res);
                    if (res.data == 1) {
                        layer.msg('审核通过', { icon: 1 }, function () {
                            location.reload();
                        });
                    }
                }
            })
        })

        //审核不通过
        $(document).on('click', "#no", function () {
            var shenhe = {
                MovelibraryId: movelibraryId,
                DocumentStatus: 3,
                ReasonForReview: $("#ReasonForReviews").val() || '',
                office_id: "1859803506630856704",
                user_id: 3,
                Token: token
            }
            $.ajax({
                url: '/Movelibrary/AuditMovelibrary',
                type: 'post',
                data: shenhe,
                success: function (d) {
                    var res = JSON.parse(d);
                    console.log(res);
                    if (res.data == 1) {
                        layer.msg('审核不通过', { icon: 1 }, function () {
                            location.reload();
                        });
                    }
                }
            })
        })


    })
}
$("#AddMovelibrarys").click(function () {
    location.href = '/Movelibrary/AddMovelibrary';
})